import time

from lxml import etree
from selenium import webdriver

from bili_homework.bili_helper import save_data


def get_page(url):
    browser = webdriver.Chrome()
    browser.get(url)
    time.sleep(3)
    html = browser.page_source
    return html


def parse_page(html):
    etree_html = etree.HTML(html)
    res = etree_html.xpath('//li[@class="bangumi-item"]')
    for item in res:
        # print(item)
        title = item.xpath('.//a[@class="bangumi-title"]/text()')[0]
        addr = item.xpath('.//a[@class="bangumi-title"]/@href')[0][2:]
        likes = item.xpath('./a/div[@class="shadow"]/text()')[0]
        # cover = item.xpath('./a/div[@class="common-lazy-img"]/img/@src')[0][2:]
        print(title)
        # print(addr)
        # print(likes)
        # print(cover)
        save_data(title, addr, likes)


def get_pages():
    for page in range(2):
        url = f'https://www.bilibili.com/anime/index/#season_version=-1&area=-1&is_finish=-1&copyright=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&order=3&st=1&sort=0&page={page + 1}'
        print(page + 1)
        html = get_page(url)
        parse_page(html)


def main():
    get_pages()


if __name__ == '__main__':
    main()
